package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.admin.api.feign.RemoteDeptService;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.common.security.util.SecurityUtils;
import com.iwomy.envpro.platform.entity.BasicOperatorEntity;
import com.iwomy.envpro.platform.entity.BasicTriggerEntity;
import com.iwomy.envpro.platform.exception.PlatformException;
import com.iwomy.envpro.platform.service.BasicTriggerService;
import com.iwomy.envpro.platform.utils.DeptFillUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 基础信息-触发器
 *
 * @author pig
 * @date 2023-09-26 16:42:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicTrigger")
@Tag(description = "basicTrigger", name = "基础信息-触发器管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicTriggerController {

	private final BasicTriggerService basicTriggerService;

	private final RemoteDeptService remoteDeptService;

	/**
	 * 分页查询
	 *
	 * @param page         分页对象
	 * @param basicTrigger 基础信息-触发器
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicTrigger_view')")
	public R getBasicTriggerPage(@ParameterObject Page page, @ParameterObject BasicTriggerEntity basicTrigger) {
		List data = remoteDeptService
				.listByDeptId(ObjectUtil.isNull(basicTrigger.getDeptId())? SecurityUtils.getUser().getDeptId() :basicTrigger.getDeptId()).getData();
		basicTrigger.setDeptId(null);

		LambdaQueryWrapper<BasicTriggerEntity> wrapper = Wrappers.lambdaQuery(basicTrigger);
		wrapper.in(ObjectUtil.isNotEmpty(data), BasicTriggerEntity::getDeptId, data);
		page = basicTriggerService.page(page, wrapper);
		DeptFillUtils.fill(page);
		return R.ok(page);
	}


	/**
	 * 通过id查询基础信息-触发器
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicTrigger_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicTriggerService.getById(id));
	}

	/**
	 * 新增基础信息-触发器
	 *
	 * @param basicTrigger 基础信息-触发器
	 * @return R
	 */
	@Operation(summary = "新增基础信息-触发器", description = "新增基础信息-触发器")
	@SysLog("新增基础信息-触发器")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicTrigger_add')")
	public R save(@RequestBody BasicTriggerEntity basicTrigger) {
		BasicTriggerEntity wrapperEntity = new BasicTriggerEntity();
		wrapperEntity.setTriggerName(basicTrigger.getTriggerName());
		LambdaQueryWrapper<BasicTriggerEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicTriggerEntity one = basicTriggerService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)) {
			throw new PlatformException("触发器名称已存在");
		}
		return R.ok(basicTriggerService.save(basicTrigger));
	}

	/**
	 * 修改基础信息-触发器
	 *
	 * @param basicTrigger 基础信息-触发器
	 * @return R
	 */
	@Operation(summary = "修改基础信息-触发器", description = "修改基础信息-触发器")
	@SysLog("修改基础信息-触发器")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicTrigger_edit')")
	public R updateById(@RequestBody BasicTriggerEntity basicTrigger) {
		BasicTriggerEntity wrapperEntity = new BasicTriggerEntity();
		wrapperEntity.setTriggerName(basicTrigger.getTriggerName());
		LambdaQueryWrapper<BasicTriggerEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicTriggerEntity one = basicTriggerService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one) && !one.getId().equals(basicTrigger.getId())) {
			throw new PlatformException("触发器名称已存在");
		}
		return R.ok(basicTriggerService.updateById(basicTrigger));
	}

	/**
	 * 通过id删除基础信息-触发器
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除基础信息-触发器", description = "通过id删除基础信息-触发器")
	@SysLog("通过id删除基础信息-触发器")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicTrigger_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(basicTriggerService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicTrigger 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicTrigger_export')")
	public List<BasicTriggerEntity> export(BasicTriggerEntity basicTrigger) {
		return basicTriggerService.list(Wrappers.query(basicTrigger));
	}
}